package com.cinema.util;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * 密码加密工具类
 */
public class PasswordUtil {
    
    private static final String SALT = "cinema_booking_system_salt_2024";
    
    /**
     * 加密密码
     */
    public static String encrypt(String password) {
        if (password == null || password.isEmpty()) {
            return null;
        }
        // 使用MD5加密，加盐
        return DigestUtils.md5Hex(password + SALT);
    }
    
    /**
     * 验证密码
     */
    public static boolean verify(String password, String encryptedPassword) {
        if (password == null || encryptedPassword == null) {
            return false;
        }
        return encrypt(password).equals(encryptedPassword);
    }
    
    /**
     * 生成随机密码
     */
    public static String generateRandomPassword(int length) {
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder password = new StringBuilder();
        
        for (int i = 0; i < length; i++) {
            int index = (int) (Math.random() * chars.length());
            password.append(chars.charAt(index));
        }
        
        return password.toString();
    }
}
